package com.shady.diskmanger.view;

import com.shady.diskmanger.dao.CreateThreadDao;
import com.shady.diskmanger.dao.FilesFunctionDao;

import java.io.File;
import java.util.Scanner;

public class FilesView {
    private static final Scanner scanner = new Scanner(System.in);
    private FilesFunctionDao filesDao = new FilesFunctionDao();
    public void print(String string) {
        System.out.println(string);
    }

    public void showHomePage() {
        while (true) {
            String show = "*******欢迎使用shady磁盘管理系统*******\n" +
                    "1.扫描重复文件\n" +
                    "2.复制文件\n" +
                    "3.传输大型文件\n" +
                    "4.恢复重复文件\n" +
                    "0.退出\n" +
                    "请输入您的选择^-^";
            print(show);
            choiceHomePage();
        }
    }

    public void choiceHomePage() {
        int choice = scanner.nextInt();
        switch (choice) {
            case 1 :
                showRepeatScannerPage();
                break;
            case 2 :
                print("功能正在开发……");
                break;
            case 3 :
                showBigFileCopyPage();
                break;
            case 4 :
                showRecoverPage();
                break;
            default:
                System.exit(1);
        }
    }

    private void showBigFileCopyPage() {
        String show = "1. 传输一个新的大型文件\t\t2. 恢复传输";
        print(show);
        choiceBigFileCopyPage();
    }

    private void choiceBigFileCopyPage() {
        int choice = scanner.nextInt();
        switch (choice) {
            case 1 :
                print("请输入要传输的文件,按0返回上一页");
                String src = scanner.next();
                if ("0".equals(src)) showBigFileCopyPage();
                File srcFile = new File(src);
                print("请输入传输到的位置,按0返回上一页");
                String dest = scanner.next();
                if ("0".equals(dest)) showBigFileCopyPage();
                File destFile = new File(dest);
                long startTime = System.currentTimeMillis();
                CreateThreadDao createThreadDao = new CreateThreadDao();
                if (!createThreadDao.createbigFileCopyThread(srcFile, destFile)) choiceBigFileCopyPage();
                System.out.println("正在传输...");
                while (!isCopyDone(srcFile, destFile));
                print("传输完成，总耗时" + (System.currentTimeMillis() - startTime) + "ms");
                showHomePage();
            case 2 :
                System.out.println("正在开发...");
        }
    }

    public boolean isCopyDone(File srcFile, File destFile) {
        destFile = new File(destFile.getPath() + "\\" + srcFile.getName());
        if (srcFile.length() == destFile.length()) {
            return true;
        }
        return false;
    }

    public void showRepeatScannerPage() {
        String show = "请输入已备份的路径,按0返回首页";
        print(show);
        choiceScannerPage();
    }

    public void choiceScannerPage() {
        String src = scanner.next();
        if ("0".equals(src)) showHomePage();
        else {
            print("正在开始扫描");
            boolean result = filesDao.scanneAllFiles(src);
            if (result) {
                print("扫描完成");
                showScannerDonePage();
            }
            showFileNotFoundError("文件夹");
            showRepeatScannerPage();
        }
    }

    private void showFileNotFoundError(String fileORFileDirectory) {
        print("未找到" + fileORFileDirectory + "，请您检查后重新输入！");
    }

    public void showScannerDonePage() {
        filesDao.getRepeatFiles()
                .stream()
                .forEach(System.out::println);
        String show = "1.清除所有重复文件\t\t" +
                "2.备份所有重复文件后删除\t\t" +
                "3.将重复文件信息导出\t\t" +
                "0.返回首页";
        print(show);
        choiceScannerDonePage();
    }

    public void choiceScannerDonePage() {
        int choice = scanner.nextInt();
        switch (choice) {
            case 1 :
                int result1 = filesDao.deleteRepeatFiles();
                if (result1 != -1) {
                    print("已经为您删除" + result1 + "个重复文件！");
                    showHomePage();
                }
                break;
            case 2 :
                print("请输入备份的路径,按0返回上一级");
                String dest = scanner.next();
                if ("0".equals(dest)) showScannerDonePage();
                int result2 = filesDao.copyRepeatFiles(dest);
                int result3 = filesDao.deleteRepeatFiles();
                if (result2 != -1) {
                    print("已经为您成功备份" + result2 + "并删除个" + result3 + "个重复文件");
                    showHomePage();
                }
                showFileNotFoundError("文件夹");
                showScannerDonePage();
                break;
            case 3 :
                print("请输入导出的路径,按0返回上一级");
                String dest2 = scanner.next();
                if ("0".equals(dest2)) showScannerDonePage();
                boolean result4 = filesDao.outPutRepeatFilesMessages(dest2);
                if (result4) {
                    print("已成功导出！");
                    showScannerDonePage();
                }
                showFileNotFoundError("文件夹");
                showScannerDonePage();
                break;
            default:
                showHomePage();
        }
    }
    public void showRecoverPage() {
        String show = "请输入已备份的路径,按0返回首页";
        print(show);
        choiceRecoverPage();
    }

    public void choiceRecoverPage() {
        String src = scanner.next();
        if ("0".equals(src)) showHomePage();
        int result = filesDao.recoverRepeatFiles(src);
        if (result != -1) {
            print("已为您恢复" + result + "个文件");
            showHomePage();
        }
        showFileNotFoundError("文件夹");
        showRecoverPage();
    }
}
